package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.ComparendoSegCont;

public class ComparendoSegContDAOImpl extends JdbcDaoSupport implements IComparendoSegContDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;
    
    private FindByRadicadoQuery findByRadicadoQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM OPE_COMPARENDO_SEG_CONT_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(ComparendoSegCont comparendoSegCont) {
		deleteOperation.execute( comparendoSegCont  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public ComparendoSegCont findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(ComparendoSegCont comparendoSegCont) {
		insertOperation.execute( comparendoSegCont );
	}

	public void update(ComparendoSegCont comparendoSegCont) {
		updateOperation.execute( comparendoSegCont );
	}

	public ComparendoSegCont findByCriteria(ComparendoSegCont  comparendoSegCont) {
		return findByCriteriaQuery.runQuery(comparendoSegCont);
	}

	public List load() {
		return findAllQuery.runQuery();
	}
	
	public List loadByRadicado(ComparendoSegCont comparendoSegCont) {
		return findByRadicadoQuery.runQuery(comparendoSegCont);
	}

	public ComparendoSegCont findByActiveState(ComparendoSegCont  comparendoSegCont) {
		return findByActiveStateQuery.runQuery(comparendoSegCont);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
//    	deleteOperation = new DeleteOperation(  getDataSource() );
//    	deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
//    	findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
//    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByRadicadoQuery = new FindByRadicadoQuery( getDataSource() );
 //   	findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO OPE_COMPARENDO_SEG_CONT_T ("
				  + " ID, "
				  + " ID_COMPARENDO, "
				  + " CODIGO, "
				  + " DETALLE, "
				  + " FECHA_SEGUIMIENTO, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "				  
				  + ") VALUES ("
			  	  + " OPE_COMPARENDO_SEG_CONT_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));			
			compile();
		}
		public void execute(ComparendoSegCont comparendoSegCont) {
			InsertOperation.this.update( new Object[]{				
				comparendoSegCont.getIdComparendo(),
				comparendoSegCont.getCodigo(),
				comparendoSegCont.getDetalle(),
				comparendoSegCont.getFechaSeguimiento(),
				comparendoSegCont.getEstado(),
				comparendoSegCont.getUsuarioCrea()				
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE OPE_COMPARENDO_SEG_CONT_T SET "					  
					  + " ID_COMPARENDO = ? ,"
					  + " CODIGO = ? ,"
					  + " DETALLE = ? ,"
					  + " FECHA_SEGUIMIENTO = ? ,"
					  + " ESTADO = ? ,"					  
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE"
				  	  + WHERE
				  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.DATE));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));			
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(ComparendoSegCont comparendoSegCont) {
			UpdateOperation.this.update( new Object[]{				
				comparendoSegCont.getIdComparendo(),
				comparendoSegCont.getCodigo(),
				comparendoSegCont.getDetalle(),
				comparendoSegCont.getFechaSeguimiento(),
				comparendoSegCont.getEstado(),				
				comparendoSegCont.getUsuarioModifica(),				
				comparendoSegCont.getId(),
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM OPE_COMPARENDO_SEG_CONT_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(ComparendoSegCont comparendoSegCont) {
			this.update( new Object[]{
				comparendoSegCont.getCodigo(),
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM OPE_COMPARENDO_SEG_CONT_T "
										+ WHERE + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			ComparendoSegCont comparendoSegCont = new ComparendoSegCont();
				comparendoSegCont.setId(new Long(rs.getLong("ID")));
				comparendoSegCont.setIdComparendo(new Long(rs.getLong("ID_COMPARENDO")));
				comparendoSegCont.setCodigo(rs.getString("CODIGO"));
				comparendoSegCont.setDetalle(rs.getString("DETALLE"));
				comparendoSegCont.setFechaSeguimiento(rs.getDate("FECHA_SEGUIMIENTO"));		
				comparendoSegCont.setEstado(rs.getString("ESTADO"));
				comparendoSegCont.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				comparendoSegCont.setFechaCrea(rs.getDate("FECHA_CREA"));		
				comparendoSegCont.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				comparendoSegCont.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
			return comparendoSegCont;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public ComparendoSegCont runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (ComparendoSegCont) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND ID_COMPARENDO = ? "
			  	  	  					//+ " AND CODIGO = ? "
			  	  	  					//+ " AND DETALLE = ? "
			  	  	  					//+ " AND FECHA_SEGUIMIENTO = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public ComparendoSegCont runQuery(ComparendoSegCont comparendoSegCont) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//comparendoSegCont.getId(),
				//comparendoSegCont.getIdComparendo(),
				//comparendoSegCont.getCodigo(),
				//comparendoSegCont.getDetalle(),
				//comparendoSegCont.getFechaSeguimiento(),
				//comparendoSegCont.getEstado(),
				//comparendoSegCont.getUsuarioCrea(),
				//comparendoSegCont.getFechaCrea(),
				//comparendoSegCont.getUsuarioModifica(),
				//comparendoSegCont.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (ComparendoSegCont) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }
    
    // ------------- FindByRadicadoQuery ----------------------------------------    
    class FindByRadicadoQuery extends BaseQueryOperation {
		private static final String sql = "SELECT SEG.* FROM OPE_COMPARENDO_SEG_CONT_T SEG INNER JOIN "
										+ " OPE_COMPARENDO_T COMP ON SEG.ID_COMPARENDO=COMP.ID " 
										+ "WHERE RADICADO=? OR '%'=?";
    	public FindByRadicadoQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(ComparendoSegCont comparendoSegCont) {
			List result =  execute( new Object[]{
					comparendoSegCont.getRadicado(),
					comparendoSegCont.getRadicado()
			});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }    

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public ComparendoSegCont runQuery(ComparendoSegCont comparendoSegCont) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (ComparendoSegCont) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
